/**
 * 
 */
package com.geezdata.cps.sdk3rd.wechat.method;

import java.util.Map;

import com.geezdata.cps.sdk3rd.wechat.request.WechatKefuRequest;
import com.geezdata.cps.sdk3rd.wechat.response.WechatKefuResult;
import com.google.common.collect.Maps;

/**
 * @author sszheng
 * @date 创建时间：2018年7月5日 下午4:23:54
 *
 */
public class WechatKefu extends BaseWechat implements Wechat<WechatKefuRequest, WechatKefuResult> {
	private static final String KEFU_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";
	
	@Override
	public WechatKefuResult execute(WechatKefuRequest request) {
		Map<String, Object> params = prepareParams(request);
		return doPost(KEFU_URL + request.getToken(), params, WechatKefuResult.class);
	}
	private Map<String, Object> prepareParams(WechatKefuRequest request) {
		Map<String, Object> map = Maps.newHashMap();
		Map<String, Object> maps = Maps.newHashMap();
		Map<String, String> type = Maps.newHashMap();
		map.put("touser", request.getOpenId());
		map.put("msgtype", request.getType());
		if("text".equals(request.getType())) {
			type.put("content", request.getContent());
			map.put(request.getType(), type);
		} else if("news".equals(request.getType())){
			type.put("media_id", request.getMediaId());
			map.put(request.getType(), type);
		}else if("miniprogrampage".equals(request.getType())){
			maps.put("title",request.getTitle());
			maps.put("appid",request.getAppid());
			maps.put("pagepath",request.getPage());
			maps.put("thumb_media_id",request.getMediaId());
			map.put("miniprogrampage",maps);
		}else if("image".equals(request.getType())){
			type.put("media_id", request.getMediaId());
			map.put(request.getType(), type);
		}else if("link".equals(request.getType())) {
			type.put("title", request.getTitle());
			type.put("description", request.getDescription());
			type.put("url", request.getUrl());
			type.put("thumb_url", request.getThumbUrl());
			map.put("link", type);
		}
		return map;
	}
}

